#include "dac.h"

void dac_init()
{
	/* Initialize DAC GPIO */
	GPIO_InitType GPIO_InitStruct;
	
	RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
	/* PA.4 DAC CH1 */
	GPIO_InitStruct.Pin       = GPIO_PIN_4;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStruct);
	
	/* Initialize DAC */
	DAC_InitType DAC_InitStruct;

    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
    /* DAC channel1 Configuration */
    DAC_InitStruct.Trigger          = DAC_TRG_SOFTWARE;
    DAC_InitStruct.WaveGen          = DAC_WAVEGEN_NOISE;
    DAC_InitStruct.LfsrUnMaskTriAmp = DAC_UNMASK_LFSRBIT0;
    DAC_InitStruct.BufferOutput     = DAC_BUFFOUTPUT_ENABLE;
    DAC_Init(DAC_CHANNEL_1, &DAC_InitStruct);
    /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.4 is
       automatically connected to the DAC converter. */
    DAC_Enable(DAC_CHANNEL_1, ENABLE);
	/* Set DAC output value */
	DAC_SetCh1Data(DAC_ALIGN_R_12BIT, 2000);
	/* Enable DAC output */
	DAC_SoftTrgEnable(DAC_CHANNEL_1, ENABLE);
}
